/***************************************************************************
* Lyrics-Player                                                            *
* Copyright (C) 2012 Andrey Derevyanko <derandr@gmail.com>                 *
*                                                                          *
* This program is free software: you can redistribute it and/or modify     *
* it under the terms of the GNU General Public License as published by     *
* the Free Software Foundation, either version 3 of the License, or        *
* (at your option) any later version.                                      *
*                                                                          *
* This program is distributed in the hope that it will be useful,          *
* but WITHOUT ANY WARRANTY; without even the implied warranty of           *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
* GNU General Public License for more details.                             *
*                                                                          *
* You should have received a copy of the GNU General Public License        *
* along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
***************************************************************************/

/*! \file coverInfoWnd.cpp
 *  \brief Window class for track information displaying implementation
 *  \author Andrey Derevyanko <derandr@gmail.com>
 *  \version 1.1
 *  \date 23/06/2012
 *  \copyright GNU Public License
 */

#include "coverInfoWnd.h"

coverInfoWnd::coverInfoWnd(QObject *parent) :
    QGridLayout(static_cast<QWidget*>(parent))
{
    isMetaDataCopied = false;
    savedMetaData.m_artist.clear();
    savedMetaData.m_title.clear();
    savedMetaData.m_album.clear();
    savedMetaData.m_year.clear();
    savedMetaData.m_genre.clear();

    coverLabel = new QLabel;
    actualCover = new QPixmap(":/pics/cover.jpg");
    artistLabel = new QLineEdit;
    titleLabel = new QLineEdit;
    albumLabel = new QLineEdit;
    yearLabel = new QLineEdit;
    genreLabel = new QLineEdit;

    editButton = new QPushButton(tr("Edit"));
    copyButton = new QPushButton(tr("Copy"));
    pasteButton = new QPushButton(tr("Paste"));
    saveButton = new QPushButton(tr("Save"));

    artistCheckBox = new QCheckBox();
    titleCheckBox = new QCheckBox();
    albumCheckBox = new QCheckBox();
    yearCheckBox = new QCheckBox();
    genreCheckBox = new QCheckBox();

    editButton->setCheckable(true);
    editButton->setChecked(false);
    setLabelsReadOnly();

    coverLabel->setMaximumSize(cover_size, cover_size);
    coverLabel->setPixmap(actualCover->scaled(cover_size, cover_size));

    QHBoxLayout *buttonsLayout = new QHBoxLayout;
    buttonsLayout->insertStretch(-1);
    buttonsLayout->addWidget(editButton);
    buttonsLayout->addWidget(copyButton);
    buttonsLayout->addWidget(pasteButton);
    buttonsLayout->addWidget(saveButton);

    addWidget(coverLabel, 0, 0, 6, 1);
    addWidget(new QLabel(tr("Artist:")), 0, 1);
    addWidget(new QLabel(tr("Title:")), 1, 1);
    addWidget(new QLabel(tr("Album:")), 2, 1);
    addWidget(new QLabel(tr("Year:")), 3, 1);
    addWidget(new QLabel(tr("Genre:")), 4, 1);
    addWidget(artistLabel, 0, 2);
    addWidget(titleLabel, 1, 2);
    addWidget(albumLabel, 2, 2);
    addWidget(yearLabel, 3, 2);
    addWidget(genreLabel, 4, 2);
    // ToDo: Add buttons after metad data save problem solving
    addWidget(artistCheckBox, 0, 3);
    addWidget(titleCheckBox, 1, 3);
    addWidget(albumCheckBox, 2, 3);
    addWidget(yearCheckBox, 3, 3);
    addWidget(genreCheckBox, 4, 3);
    addLayout(buttonsLayout, 5, 1, 1, 2);

    connect(editButton, SIGNAL(clicked()), SLOT(setLabelsReadOnly()));
    connect(copyButton, SIGNAL(clicked()), SLOT(copyMetaData()));
    connect(pasteButton, SIGNAL(clicked()), SLOT(pasteMetaData()));
    connect(saveButton, SIGNAL(clicked()), SLOT(saveMetaData()));
}

void coverInfoWnd::newFileInfo(trackMetaData_t trackMetaData)
{
    QString filePath = trackMetaData.m_filePath;
    filePath.resize(filePath.lastIndexOf('/')+1);
    filePath.append(tr("cover.jpg"));
    if (QFile::exists(filePath))
        actualCover->load(filePath);
    else
        actualCover->load(":/pics/cover.jpg");
    coverLabel->setPixmap(actualCover->scaled(cover_size, cover_size));
    artistLabel->setText(trackMetaData.m_artist);
    titleLabel->setText(trackMetaData.m_title);
    albumLabel->setText(trackMetaData.m_album);
    yearLabel->setText(trackMetaData.m_year);
    genreLabel->setText(trackMetaData.m_genre);
}

void coverInfoWnd::setLabelsReadOnly()
{
    bool state = !(editButton->isChecked());
    artistLabel->setReadOnly(state);
    titleLabel->setReadOnly(state);
    albumLabel->setReadOnly(state);
    yearLabel->setReadOnly(state);
    genreLabel->setReadOnly(state);
}

void coverInfoWnd::copyMetaData()
{
    if (!(editButton->isChecked())) return;
    if (artistCheckBox->isChecked()) savedMetaData.m_artist = artistLabel->text();
    if (titleCheckBox->isChecked()) savedMetaData.m_title = titleLabel->text();
    if (albumCheckBox->isChecked()) savedMetaData.m_album = albumLabel->text();
    if (yearCheckBox->isChecked()) savedMetaData.m_year = yearLabel->text();
    if (genreCheckBox->isChecked()) savedMetaData.m_genre = genreLabel->text();
    isMetaDataCopied = true;
}

void coverInfoWnd::pasteMetaData()
{
    if (!(editButton->isChecked())) return;
    if (!isMetaDataCopied) return;
    if (artistCheckBox->isChecked()) artistLabel->setText(savedMetaData.m_artist);
    if (titleCheckBox->isChecked()) titleLabel->setText(savedMetaData.m_title);
    if (albumCheckBox->isChecked()) albumLabel->setText(savedMetaData.m_album);
    if (yearCheckBox->isChecked()) yearLabel->setText(savedMetaData.m_year);
    if (genreCheckBox->isChecked()) genreLabel->setText(savedMetaData.m_genre);
}

void coverInfoWnd::saveMetaData()
{
    trackMetaData_t data;
    if (!(editButton->isChecked())) return;
    data.m_artist = artistLabel->text();
    data.m_title = titleLabel->text();
    data.m_album = albumLabel->text();
    data.m_year = yearLabel->text();
    data.m_genre = genreLabel->text();
    emit saveEditedMetaData(data);
}
